-- The MIT License
-- 
-- Copyright (c) 2008 Matthew Maravillas, Patrick Quinn
-- 
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
-- 
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
-- 
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-- THE SOFTWARE.

local Class = import('/lua/system/class.lua').Class
local mapUtils = import('/mods/koth/hook/lua/MapUtilities.lua')

local FACTION_PREFIXES = {'UEBK', 'UABK', 'URBK'}
local FLAG_UNIT_NUMBER = '1001'
local FLAG_CIRCLE_RADIUS = 2.5
local HILL_UNIT_BLUEPRINT = 'KOTH1001'

local function GetFlagPosition(armyIndex, armyCount, center)
    local position = {}
    
    local angle = (math.rad(360) / armyCount) * (armyIndex + 1)
    
    position.x = math.cos(angle) * FLAG_CIRCLE_RADIUS + center.x
    position.z = math.sin(angle) * FLAG_CIRCLE_RADIUS + center.z
    
    position.y = mapUtils.GetMapHeight(position.x, position.z)
    
    return position
end

Flag = Class(){
    __init = function(self, armyName, armyIndex, armyCount, position)
        self.flagPosition = GetFlagPosition(armyIndex, armyCount, position)
        self.hillPosition = position
        self.armyName = armyName
        
        local brain = GetArmyBrain(self.armyName)
        
        self.flagUnitBlueprint = FACTION_PREFIXES[brain:GetFactionIndex()] .. FLAG_UNIT_NUMBER
        
        brain.flag = self
        
        brain.GetFlag = function(self)
            return self.flag
        end
        
        brain.hasFlag = true
    end,
    
    IsFlagSpawned = function(self)
        return self.unit and not self.unit:IsDead()
    end,
    
    IsHillUnitSpawned = function(self)
        return self.hillUnit and not self.hillUnit:IsDead()
    end,
    
    SpawnUnit = function(self, blueprint, position)
        LOG('Flag:Activate: Spawning ' .. blueprint .. 
                                ' for ' .. self.armyName .. 
                                ' at ' .. position.x .. ', ' .. position.y .. ', ' .. position.z)
        
        local unit = CreateUnitHPR(blueprint,
                      self.armyName,
                      position.x, position.y, position.z,
                      0, 0, 0)
        
        unit:SetCanTakeDamage(false)
        unit:SetDoNotTarget(true)
        
        return unit
    end,
    
    Activate = function(self)
        if not self:IsFlagSpawned() then
            self.unit = self:SpawnUnit(self.flagUnitBlueprint, self.flagPosition)
        end
        
        if not self:IsHillUnitSpawned() then
            self.hillUnit = self:SpawnUnit(HILL_UNIT_BLUEPRINT, self.hillPosition)
        end
    end,
    
    -- This method hasn't been tested yet.
    -- It would be nice to find a way to remove the flag without appearing
    -- to destroy it.
    Deactivate = function(self)
        if self:IsFlagSpawned() then
            LOG('Flag:Deactivate: Deactivating flag for ' .. self.armyName)
            self.unit:Destroy()
        end
        
        if self:IsHillUnitSpawned() then
            LOG('Flag:Deactivate: Deactivating hill unit for ' .. self.armyName)
            self.hillUnit:Destroy()
        end
    end,
}